
<html><head>
<title>automatic_differentiation - flibs</title>
<style type="text/css"><!--
    HTML {
	background: 	#FFFFFF;
	color: 		black;
    }
    BODY {
	background: 	#FFFFFF;
	color:	 	black;
    }
    DIV.doctools {
	margin-left:	10%;
	margin-right:	10%;
    }
    DIV.doctools H1,DIV.doctools H2 {
	margin-left:	-5%;
    }
    H1, H2, H3, H4 {
	margin-top: 	1em;
	font-family:	sans-serif;
	font-size:	large;
	color:		#005A9C;
	background: 	transparent;
	text-align:		left;
    }
    H1.title {
	text-align: center;
    }
    UL,OL {
	margin-right: 0em;
	margin-top: 3pt;
	margin-bottom: 3pt;
    }
    UL LI {
	list-style: disc;
    }
    OL LI {
	list-style: decimal;
    }
    DT {
	padding-top: 	1ex;
    }
    UL.toc,UL.toc UL, UL.toc UL UL {
	font:		normal 12pt/14pt sans-serif;
	list-style:	none;
    }
    LI.section, LI.subsection {
	list-style: 	none;
	margin-left: 	0em;
	text-indent:	0em;
	padding: 	0em;
    }
    PRE {
	display: 	block;
	font-family:	monospace;
	white-space:	pre;
	margin:		0%;
	padding-top:	0.5ex;
	padding-bottom:	0.5ex;
	padding-left:	1ex;
	padding-right:	1ex;
	width:		100%;
    }
    PRE.example {
	color: 		black;
	background: 	#f5dcb3;
	border:		1px solid black;
    }
    UL.requirements LI, UL.syntax LI {
	list-style: 	none;
	margin-left: 	0em;
	text-indent:	0em;
	padding:	0em;
    }
    DIV.synopsis {
	color: 		black;
	background: 	#80ffff;
	border:		1px solid black;
	font-family:	serif;
	margin-top: 	1em;
	margin-bottom: 	1em;
    }
    UL.syntax {
	margin-top: 	1em;
	border-top:	1px solid black;
    }
    UL.requirements {
	margin-bottom: 	1em;
	border-bottom:	1px solid black;
    }
--></style>
</head>
<! -- Generated from file 'computing/automdiff.man' by tcllib/doctools with format 'html'
   -->
<! -- Copyright &copy; 2006 Arjen Markus &lt;arjenmarkus@sourceforge.net&gt;
   -->
<! -- CVS: $Id: automdiff.html,v 1.3 2013/05/13 08:03:15 knystrom Exp $ automatic_differentiation.n
   -->
<body><div class="doctools">
<h1 class="title">automatic_differentiation(n) 1.0  &quot;flibs&quot;</h1>
<div id="name" class="section"><h2><a name="name">Name</a></h2>
<p>automatic_differentiation - Implement the &quot;automatic differentation&quot; technique</p>
</div>
<div id="toc" class="section"><h2><a name="toc">Table Of Contents</a></h2>
<ul class="toc">
<li class="section"><a href="#toc">Table Of Contents</a></li>
<li class="section"><a href="#synopsis">Synopsis</a></li>
<li class="section"><a href="#section1">Description</a></li>
<li class="section"><a href="#section2">EXAMPLE</a></li>
<li class="section"><a href="#section3">DATA TYPES AND ROUTINES</a></li>
<li class="section"><a href="#section4">POSSIBLE EXTENSIONS</a></li>
<li class="section"><a href="#copyright">Copyright</a></li>
</ul>
</div>
<div id="synopsis" class="section"><h2><a name="synopsis">Synopsis</a></h2>
<div class="synopsis">
<ul class="syntax">
<li><a href="#1"><b class="cmd">use automatic_differentiation</b></a></li>
<li><a href="#2"><b class="cmd">type(AUTODERIV)</b></a></li>
<li><a href="#3"><b class="cmd">x = derivvar( value )</b></a></li>
<li><a href="#4"><b class="cmd">call find_root( f, xinit, tolerance, root, found )</b></a></li>
<li><a href="#5"><b class="cmd">call find_root_iter( fvalue, root, tolerance, found )</b></a></li>
</ul>
</div>
</div>
<div id="section1" class="section"><h2><a name="section1">Description</a></h2>
<p>The <em>automatic_differentiation</em> module defines a derived
type that enables you (via overloading common mathematical
functions and operators) to:</p>
<ul class="itemized">
<li><p>Define a mathematical function in the usual way</p></li>
<li><p>Evaluate that function <em>and</em> its first derivative at the same
time</p></li>
</ul>
<p>without having to program that first derivative.</p>
<p>The module uses real numbers of the kind &quot;wp&quot; as defined in the
auxiliary module <em>select_precision</em>.</p>
<p>(I was pointed to this technique by Simon Geard, a couple of years ago,
in the context of one of the many language shootouts on the Internet.)</p>
</div>
<div id="section2" class="section"><h2><a name="section2">EXAMPLE</a></h2>
<p>In numerical methods like Newton-Raphson for finding a root of the
equation &quot;f(x) = 0&quot;, you need the first derivative:</p>
<pre class="example">
    x(k+1)  =  x(k)  - f(x(k)) / f'(x(k))
</pre>
<p>Rather than implementing the function and its first derivatives as
separate functions, using this module you can dispense with manually
determining the first derivative:</p>
<pre class="example">
program root
    use automatic_differentation
    type(AUTODERIV) :: x
    type(AUTODERIV) :: res
    integer         :: i
    !
    ! First estimate
    !
    x = derivvar( 1.0_wp )
    do i = 1,10
        res = f(x)
        x = x - res.v / res.dv
    enddo
    write(*,*) 'Root: ', x.v
contains
type(AUTODERIV) function f(x)
    type(AUTODERIV) :: x
    f = x + cos(x)
end function f
end program
</pre>
</div>
<div id="section3" class="section"><h2><a name="section3">DATA TYPES AND ROUTINES</a></h2>
<p>The module defines a single data type, AUTODERIV, and one specific
function, derivvar().</p>
<dl class="definitions">
<dt><a name="1"><b class="cmd">use automatic_differentiation</b></a></dt>
<dd><p>The name of the module</p></dd>
<dt><a name="2"><b class="cmd">type(AUTODERIV)</b></a></dt>
<dd><p>The type has two fields:</p>
<dl class="arguments">
<dt>real(wp) <i class="arg">v</i></dt>
<dd><p>The value of the variable/function (or any intermediate result)</p></dd>
<dt>real(wp) <i class="arg">dv</i></dt>
<dd><p>The first derivative</p></dd>
</dl></dd>
<dt><a name="3"><b class="cmd">x = derivvar( value )</b></a></dt>
<dd><p>Use this function to properly initialise the program
variable x as a &quot;mathematical&quot; variable. (It sets the derivative
of x to 1, because otherwise it would be regarded as a constant).</p>
<dl class="arguments">
<dt>real(wp) <i class="arg">value</i></dt>
<dd><p>The value of the &quot;mathematical&quot; variable.</p></dd>
</dl></dd>
<dt><a name="4"><b class="cmd">call find_root( f, xinit, tolerance, root, found )</b></a></dt>
<dd><p>This subroutine is a simple implementation of the Newton-Raphson
method to find roots. The function f takes one argument, x.</p>
<dl class="arguments">
<dt>type(AUTODERIV) function <i class="arg">f(x)</i></dt>
<dd><p>The function must have the interface:</p>
<pre class="example">
    interface
        function f(x)
            use automatic_differentiation_type
            type(AUTODERIV), intent(in) :: x
            type(AUTODERIV)             :: f
        end function
    end interface
</pre>
<p>Its return value is the function value at point x and its first
derivative.</p></dd>
<dt>type(AUTODERIV), intent(in) <i class="arg">xinit</i></dt>
<dd><p>Initial estimate of the root - the end result may depend on this choice.</p></dd>
<dt>real(wp) <i class="arg">tolerance</i></dt>
<dd><p>Maximum allowable error in the absolute value of the root. If the
difference between the old and the new estimate is smaller than this
value, the iteration stops.</p></dd>
<dt>type(AUTODERIV), intent(out) <i class="arg">root</i></dt>
<dd><p>Final estimate of the root.</p></dd>
<dt>logical <i class="arg">found</i></dt>
<dd><p>Indicator whether a root was found or not. There is a maximum
number of iterations and the root must not grow too large.</p></dd>
</dl></dd>
<dt><a name="5"><b class="cmd">call find_root_iter( fvalue, root, tolerance, found )</b></a></dt>
<dd><p>This subroutine performs a single iteration in the Newton-Raphson
method to find roots. It can be used to implement a more general version
of <em>find_root</em>, for instance if the interface to the function
contains one or more asjustable parameters.</p>
<dl class="arguments">
<dt>type(AUTODERIV) <i class="arg">fvalue</i></dt>
<dd><p>The value of the function (plus its first derivative)</p></dd>
<dt>type(AUTODERIV), intent(in) <i class="arg">root</i></dt>
<dd><p>Current estimate of the root</p></dd>
<dt>real(wp) <i class="arg">tolerance</i></dt>
<dd><p>Maximum allowable error in the absolute value of the root. If the
difference between the old and the new estimate is smaller than this
value, the indicator <em>found</em> is set to true.</p></dd>
<dt>logical <i class="arg">found</i></dt>
<dd><p>Indicator whether a root was found or not.</p></dd>
</dl></dd>
</dl>
</div>
<div id="section4" class="section"><h2><a name="section4">POSSIBLE EXTENSIONS</a></h2>
<p>You can extend the module in at least two ways:</p>
<ul class="itemized">
<li><p>Determine the second derivative, the third and so on. This is
straightforward enough, but the formulae will get fairly complex after
the second derivative.</p></li>
<li><p>Determine the derivative of a function of two or more variables. This
will require a bit more: such functions have a vector-valued
derivative and each independent variable will have to have a
vector-valued derivative as well. For instance:</p>
<p>A function f(x,y) evaluated at (1,2), would take as the actual arguments
x = (1,1,0) (so no variation in the second direction) and y = (2,0,1)
(no variation in the first direction).</p></li>
</ul>
</div>
<div id="copyright" class="section"><h2><a name="copyright">Copyright</a></h2>
<p>Copyright &copy; 2006 Arjen Markus &lt;arjenmarkus@sourceforge.net&gt;</p>
</div>
</div></body></html>